﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BfLib.Sys
{
    public class BfTimer
    {
        private DateTime _dt1, _dt2;
        private bool _secondDTIsSet=false;
        TimeSpan ts;

        public BfTimer()
        {
            _dt1 = DateTime.Now;
        }

        public BfTimer(DateTime start)
        {
            _dt1 = start;
        }

        public BfTimer(DateTime start, DateTime finish)
        {
            _dt1 = start;
            _dt2 = finish;
            _secondDTIsSet = true;
            ts = _dt2 - _dt1;
        }

        public void Stop()
        {
            _dt2 = DateTime.Now;
            _secondDTIsSet = true;
            ts = _dt2 - _dt1;
        }

        public string FormattedString
        {
            get
            {
                if(!_secondDTIsSet)
                {
                    _dt2 = DateTime.Now;
                    ts = _dt2 - _dt1;
                }

                return String.Format("{0} days, {1} hours, {2} mins, {3} secs, {4} ms", ts.Days, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
            }
        }
    }
}
